﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Database
{
    #region DRMasters
    public class DRMasters : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        SqlABParameter sqlP2;
        string type;
        string code;

        public DRMasters(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.Type);
            sqlP2 = new SqlABParameter("@p51", PL_Masters.Code);

            qB.AddSelect();
            qB.AddFrom(AM_Locks.Name);
            qB.AddCompare(PL_Masters.Type, sqlP1);
            qB.AddCompare(PL_Masters.Code, sqlP2);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = type;
            sqlP2.Value = code;
        }

        public bool Find(string type, string code)
        {
            this.type = type;
            this.code = code;
            return base.Find();
        }

    }
    #endregion
    #region DRMastersByTaxNo
    public class DRMastersByTaxNo : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string taxNo;

        public DRMastersByTaxNo(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.TaxNo);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.TaxNo, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = taxNo;
        }

        public bool Find(string taxNo)
        {
            this.taxNo = taxNo;
            return base.Find();
        }

    }
    #endregion
    #region DRMastersByFiscalCode
    public class DRMastersByFiscalCode : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string fiscalcode;

        public DRMastersByFiscalCode(bool updater)
            : base(updater)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_Masters.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Masters.FiscalCode);

            qB.AddSelect();
            qB.AddFrom(PL_Masters.Name);
            qB.AddCompare(PL_Masters.FiscalCode, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = fiscalcode;
        }

        public bool Find(string fiscalCode)
        {
            this.fiscalcode = fiscalCode;
            return base.Find();
        }

    }
    #endregion
    #region DRTEmplatesByInvoice
    public class DRTemplatesByInvoice : DataReaderUpdater
    {
        public DRTemplatesByInvoice(SqlABConnection sqlCN, bool updater)
            : base(sqlCN, updater)
        {
            myTable = PL_Templates.Name;
        }

        public bool Find(string code)
        {
            //string sCode = string.Concat("%", ((int)code).ToString("00"), "%");
            return base.Find();
        }

        protected override void CreateConnection()
        {
            System.Diagnostics.Debug.Assert(false, "CreateConnection");
        }

        protected override void SetParameters()
        {
            throw new NotImplementedException();
        }

        //protected override string CreateQuery()
        //{
        //    QueryBuilder qb = new QueryBuilder();
        //    qb.AddSelectAll(PL_Templates.Name);
        //    qb.AddFrom(PL_Templates.Name);
        //    qb.AddCompare(PL_Templates.Combine, "@p50", "LIKE");
        //    return qb.Query;
        //}
    }
    #endregion
    #region DRTemplates
    public class DRTemplates : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string code;

        public DRTemplates(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Templates.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_Templates.Code);

            qB.AddSelect();
            qB.AddFrom(AM_Locks.Name);
            qB.AddCompare(PL_Templates.Code, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = code;
        }
    }
    #endregion
    #region RRSentences
    public class RRSentences : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        int code;

        public RRSentences(bool upater)
            : base(upater)
        {}

        protected override void CreateConnection()
        {
            myTable = PL_Sentences.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_Sentences.ID);

            qB.AddSelect();
            qB.AddFrom(PL_Sentences.Name);
            qB.AddCompare(PL_Sentences.ID, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(int code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = code;
        }
    }
    #endregion
    #region DRMasterType
    public class DRMasterType : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string code;

        public DRMasterType()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_MasterType.Code);

            qB.AddSelect();
            qB.AddFrom(PL_MasterType.Name);
            qB.AddCompare(PL_MasterType.Code, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string code)
        {
            this.code = code;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = code;
        }
    }
    #endregion
    #region DRMasterType
    public class DRMasterTypeByType : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        MasterTypes type;

        public DRMasterTypeByType()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p50", PL_MasterType.Code);

            qB.AddSelect();
            qB.AddFrom(PL_MasterType.Name);
            qB.AddCompare(PL_MasterType.MasterType, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(MasterTypes type)
        {
            this.type = type;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = (int)type;
        }
    }
    #endregion
    #region RRMasterType
    public class RRMasterType : DataReaderUpdater
    {
        public RRMasterType()
            : base(false)
        {}

        protected override void CreateConnection()
        {
            myTable = PL_MasterType.Name;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare(PL_MasterType.MasterType, ((int)MasterTypes.E_PurchaseInvoice).ToString(), "!=");
            qb.AddCompare(PL_MasterType.MasterType, ((int)MasterTypes.E_SaleInvoice).ToString(), "!=");
            qb.AddOrder(PL_MasterType.Code);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
        }
    }
    #endregion
    #region DRTemplates
    public class DRTitle : DataReaderUpdater
    {
        SqlABParameter sqlP1;
        string title;

        public DRTitle(bool updater)
            : base(updater)
        { }

        protected override void CreateConnection()
        {
            myTable = PL_Titles.Name;

            QueryBuilder qB = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", PL_Titles.Title);

            qB.AddSelect();
            qB.AddFrom(AM_Locks.Name);
            qB.AddCompare(PL_Titles.Title, sqlP1);

            sqlCM = new SqlABCommand(qB.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(string title)
        {
            this.title = title;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = title;
        }
    }
    #endregion
}
